package xyz.zhzby.cyzx.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.zhzby.cyzx.cms.entity.ArticleChannel;
import xyz.zhzby.cyzx.cms.entity.Channel;
import xyz.zhzby.cyzx.cms.mapper.ArticleChannelMapper;
import xyz.zhzby.cyzx.cms.service.IArticleChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhzby.cyzx.cms.service.IChannelService;
import xyz.zhzby.cyzx.security.entity.User;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.DataLogInfo;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章与文章分类关联 服务实现类
 * </p>
 *
 * @author zby
 * @since 2021-12-10
 */
@Service
public class ArticleChannelServiceImpl extends ServiceImpl<ArticleChannelMapper, ArticleChannel> implements IArticleChannelService {

    @Autowired
    private IChannelService channelService;


    @Override
    public void linkArticleChannel(Long id, List<String> channelIds) {
        removeByArticleId(id);

        User subject = CyzxSecurityContextHolder.getSubject();
        for (String channelId : channelIds) {
            ArticleChannel articleChannel = new ArticleChannel();
            articleChannel.setArticleId(id);
            if (!(channelId.length() == 19 && Pattern.compile("^[0-9]*$").matcher(channelId).matches())) {
                Channel channel = new Channel();
                channel.setName(channelId);
                channel.setUserId(subject.getId());
                subject.fillCreateInfo(channel, DataLogInfo.USER_FIELD_ACTUAL_NAME);
                channelService.save(channel);
                articleChannel.setChannelId(channel.getId());
            } else {
                articleChannel.setChannelId(Long.valueOf(channelId));
            }
            subject.fillCreateInfo(articleChannel, DataLogInfo.USER_FIELD_ACTUAL_NAME);
            baseMapper.insert(articleChannel);
        }
    }

    @ApiOperation("根据文章ID删除关联")
    private void removeByArticleId(Long articleId) {
        LambdaQueryWrapper<ArticleChannel> qw = new LambdaQueryWrapper<>();
        qw.eq(ArticleChannel::getArticleId, articleId);
        baseMapper.delete(qw);
    }

    @Override
    public Integer getArticleCountByChannelId(Long id) {
        LambdaQueryWrapper<ArticleChannel> qw = new LambdaQueryWrapper<>();
        qw.eq(ArticleChannel::getChannelId, id);
        return baseMapper.selectCount(qw);
    }

    @Override
    public void deleteLinkByChannelId(Long channelId) {
        LambdaQueryWrapper<ArticleChannel> qw = new LambdaQueryWrapper<>();
        qw.eq(ArticleChannel::getChannelId, channelId);
        baseMapper.delete(qw);
    }

    @Override
    public List<Long> getArticleIdsByChannelId(Long channelID) {
        LambdaQueryWrapper<ArticleChannel> qw = new LambdaQueryWrapper<>();
        qw.eq(ArticleChannel::getChannelId, channelID);
        qw.select(ArticleChannel::getArticleId);
        List<ArticleChannel> articleChannels = baseMapper.selectList(qw);
        return articleChannels.stream().map(ArticleChannel::getArticleId).collect(Collectors.toList());
    }
}
